package com.zhongke.cotrun.mapper;


import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.zhongke.cotrun.model.pojo.NeedInfo;


public interface NeedInfoMapper {

	/**
	 * 查询指定用户需求信息
	 * @param userId用户表主键
	 * @param searchKey检索关键词
	 * @return
	 */
    public List<NeedInfo> selectAllByMine(@Param("userId") Long userId,@Param("searchKey") String searchKey);
   
    /**
     * 前端检索用户需求信息
     * @param type 需求类型
     * @param technicalFieldId 技术领域主键
     * @param province 省主键
     * @param city 市主键
     * @param searchKey 检索关键词
     * @param sort 排序，无或空，默认降，'up'为升，'down'为降
     * @return
     */
    public List<NeedInfo> selectAll(@Param("type") String type,@Param("technicalFieldId") String technicalFieldId,
    		@Param("province") String province,@Param("city") String city,@Param("searchKey") String searchKey,@Param("sort") String sort);
    /**
     * 管理后台检索用户需求信息
     * @param type 需求类型
     * @param technicalFieldId 技术领域主键
     * @param province 省主键
     * @param city 市主键
     * @param searchKey 检索关键词
     * @param state 状态
     * @param endDateS 截止时间（始）
     * @param endDateE 截止时间（止）
     * @return
     */
    public List<NeedInfo> selectAllByManager(@Param("type") String type,@Param("technicalFieldId") String technicalFieldId,
    		@Param("province") String province,@Param("city") String city,@Param("searchKey") String searchKey,@Param("state") String state,
    		@Param("endDateS") String endDateS,@Param("endDateE") String endDateE);
    /**
     * 根据id查看需求详情
     *
     * @param id 需求主键
     * @return
     */
	@Select("SELECT * from wx_need_info WHERE id=#{id} and del_flag='0' ")
	public NeedInfo selectDetailById(Long id);
	 /**
     * 按状态统计
     * @param state 状态
     * @return
     */
	@Select("select count(id) from wx_need_info where state = #{state} and del_flag = '0'")
	public int queryUnApprovalCounts(@Param("state") String state);
    /**
     * 新增
     * @param  对象
     * @return
     */
     @Insert("insert into wx_need_info " +
             "(user_info_id,title,note,end_date,type,technical_field_id,technical_field_desc,state,key_word,budget,create_by,create_date,del_flag)" +
             " values (#{userInfoId},#{title},#{note},#{endDate},#{type},#{technicalFieldId},#{technicalFieldDesc},#{state}," +
             "#{keyWord},#{budget},#{createBy},now(),'0')")
     public boolean insert(NeedInfo needInfo);
     /**
      * 修改
      * @param 对象
      * @return
      */
     @Update("update wx_need_info set " +
              "title=#{title},note=#{note},end_date=#{endDate},type=#{type},technical_field_id=#{technicalFieldId},technical_field_desc=#{technicalFieldDesc},"
              + "state=#{state},key_word=#{keyWord},budget=#{budget},update_by=#{updateBy},update_date=now()" +
                 " where id=#{id} and user_info_id=#{userInfoId} and (state='2' or state='0')")
     public boolean update(NeedInfo needInfo);
     
     @Update("update wx_need_info set state='3'" +
    		  "where DATE_FORMAT(end_date,'%Y%m%d') < DATE_FORMAT(NOW(),'%Y%m%d') and (state = '1' or state = '0')")
     public int cronJobsUpdateState3();
     /**
      * 删除
      * @param id 需求主键
      * @param userInfoId 用户表主键
      * @return
      */
      @Update("update wx_need_info set del_flag='1',update_by=#{userInfoId},update_date=now() where id=#{id} and user_info_id=#{userInfoId}")
      public boolean deleteByID(@Param("id") Long id,@Param("userInfoId") Long userInfoId);
      /**
       * 管理后台删除
       * @param id 需求主键
       * @param userId 更新人
       * @return
       */
       @Update("update wx_need_info set del_flag='1',approval_by=#{userId},approval_date=now() where id in (${id})")
       public boolean managerDeleteByID(@Param("id") String id,@Param("userId") Long userId);
       
      /**
       * 下线
       * @param id 需求主键
       * @param userInfoId 用户表主键
       * @return
       */
      @Update("update wx_need_info set state='3',update_by=#{userInfoId},update_date=now() where id=#{id} and state='1' and user_info_id=#{userInfoId}")
      public boolean offline(@Param("id") Long id,@Param("userInfoId") Long userInfoId);
      /**
       * 后台下线
       * @param id 需求主键
       * @param userId 更新人
       * @return
       */
      @Update("update wx_need_info set state='3',approval_by=#{userId},approval_date=now() where id=#{id} and state='1'")
      public boolean managerOffline(@Param("id") Long id,@Param("userId") Long userId);
      /**
       * 后台审核
       * @param id 需求主键
       * @param userId 更新人
       * @param state 需求状态
       * @param remarks 备注
       * @return
       */
      @Update("update wx_need_info set state=#{state},approval_opinion=#{remarks},approval_by=#{userId},approval_date=now() where id=#{id} and state<>'3'")
      public boolean managerCheck(@Param("id") Long id,@Param("userId") Long userId,@Param("state") String state,@Param("remarks") String remarks);
}
